package com.east.sng.dao;

import com.east.sng.entity.param.UserParam;
import com.east.sng.entity.result.UserFind;
import com.east.sng.entity.result.UserInfo;
import com.east.sng.utils.RedisCache;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author east
 * @date: 2020/12/27
 * @Description:
 * @Version: 1.0
 **/
@Repository
@CacheNamespace(implementation = RedisCache.class)
public interface UserDao {

    /**
     * 查询全部用户以及角色信息
     * @param params
     * @return
     */
    List<UserInfo> selectUserRoles(Map<String, Object> params);

    /**
     * 登录验证
     * @param userName 用户名
     * @return 用户名和密码实例对象
     */
    @Select("select u.userid,u.username,u.userpassword,u.salt,u.role_id from `user` u where u.flag = 0 and u.username = #{userName} ")
    UserFind selectUser(String userName);

    /**
     * 添加用户
     * @param user 用户实例对象
     */
    @Insert("insert into `user`(username, userpassword, salt) values(#{userName} ,#{userPassword}, #{salt} )")
    void insertUser(UserParam user);

    /**
     * 修改密码
     * @param user 用户实例对象
     */
    @Update("update `user` set userpassword=#{userPassword}, salt = #{salt} where username=#{userName} ")
    void updateUserPassword(UserParam user);

    /**
     * 修改用户等级
     * @param roleId 用户角色id
     * @param userId 用户id
     */
    @Update("update `user` set role_id = #{roleId} where userid = #{userId}")
    Integer updateUserRole(Integer roleId, Integer userId);

    /**
     * 删除或恢复用户
     * @param userId 用户id
     * @param flag 删除标志
     * @return 受影响行数
     */
    @Delete("update `user` set flag = #{flag} where userid = #{userId} ")
    Integer deleteOrRecoverUser(@Param("userId") Integer userId,@Param("flag") Integer flag);

    /**
     * 批量删除或恢复用户
     * @param userIds 用户id列表
     * @param flag 删除标志
     * @return 受影响行数
     */
    @Delete({"<script>","update `user` set flag = #{flag} where userid in",
            "<foreach collection='list' item='userId' open='(' separator=',' close=')'>",
            "#{userId}",
            "</foreach>",
            "</script>"})
    Integer batchDeleteOrRecoverUsers(@Param("list") List<Integer> userIds,@Param("flag") Integer flag);

}
